<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
<title>DSF2FLAC: main.cpp Source File</title>

<link href="tabs.css" rel="stylesheet" type="text/css"/>
<link href="doxygen.css" rel="stylesheet" type="text/css" />

<link href="search/search.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="search/search.js"></script>
<script type="text/javascript">
  $(document).ready(function() { searchBox.OnSelectItem(0); });
</script>

</head>
<body>
<div id="top"><!-- do not remove this div! -->


<div id="titlearea">
<table cellspacing="0" cellpadding="0">
 <tbody>
 <tr style="height: 56px;">
  
  
  <td style="padding-left: 0.5em;">
   <div id="projectname">DSF2FLAC
   
   </div>
   
  </td>
  
  
  
 </tr>
 </tbody>
</table>
</div>

<!-- Generated by Doxygen 1.7.6.1 -->
<script type="text/javascript">
var searchBox = new SearchBox("searchBox", "search",false,'Search');
</script>
  <div id="navrow1" class="tabs">
    <ul class="tablist">
      <li><a href="index.html"><span>Main&#160;Page</span></a></li>
      <li><a href="pages.html"><span>Related&#160;Pages</span></a></li>
      <li><a href="annotated.html"><span>Classes</span></a></li>
      <li class="current"><a href="files.html"><span>Files</span></a></li>
      <li>
        <div id="MSearchBox" class="MSearchBoxInactive">
        <span class="left">
          <img id="MSearchSelect" src="search/mag_sel.png"
               onmouseover="return searchBox.OnSearchSelectShow()"
               onmouseout="return searchBox.OnSearchSelectHide()"
               alt=""/>
          <input type="text" id="MSearchField" value="Search" accesskey="S"
               onfocus="searchBox.OnSearchFieldFocus(true)" 
               onblur="searchBox.OnSearchFieldFocus(false)" 
               onkeyup="searchBox.OnSearchFieldChange(event)"/>
          </span><span class="right">
            <a id="MSearchClose" href="javascript:searchBox.CloseResultsWindow()"><img id="MSearchCloseImg" border="0" src="search/close.png" alt=""/></a>
          </span>
        </div>
      </li>
    </ul>
  </div>
  <div id="navrow2" class="tabs2">
    <ul class="tablist">
      <li><a href="files.html"><span>File&#160;List</span></a></li>
      <li><a href="globals.html"><span>File&#160;Members</span></a></li>
    </ul>
  </div>
</div>
<div class="header">
  <div class="headertitle">
<div class="title">main.cpp</div>  </div>
</div><!--header-->
<div class="contents">
<div class="fragment"><pre class="fragment"><a name="l00001"></a>00001 <span class="comment">/*</span>
<a name="l00002"></a>00002 <span class="comment"> * dsf2flac - http://code.google.com/p/dsf2flac/</span>
<a name="l00003"></a>00003 <span class="comment"> *</span>
<a name="l00004"></a>00004 <span class="comment"> * A file conversion tool for translating dsf dsd audio files into</span>
<a name="l00005"></a>00005 <span class="comment"> * flac pcm audio files.</span>
<a name="l00006"></a>00006 <span class="comment"> *</span>
<a name="l00007"></a>00007 <span class="comment"> * Copyright (c) 2013 by respective authors.</span>
<a name="l00008"></a>00008 <span class="comment"> *</span>
<a name="l00009"></a>00009 <span class="comment"> * This program is free software; you can redistribute it and/or modify</span>
<a name="l00010"></a>00010 <span class="comment"> * it under the terms of the GNU General Public License as published by</span>
<a name="l00011"></a>00011 <span class="comment"> * the Free Software Foundation; either version 2 of the License, or</span>
<a name="l00012"></a>00012 <span class="comment"> * (at your option) any later version.</span>
<a name="l00013"></a>00013 <span class="comment"> *</span>
<a name="l00014"></a>00014 <span class="comment"> * This program is distributed in the hope that it will be useful,</span>
<a name="l00015"></a>00015 <span class="comment"> * but WITHOUT ANY WARRANTY; without even the implied warranty of</span>
<a name="l00016"></a>00016 <span class="comment"> * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the</span>
<a name="l00017"></a>00017 <span class="comment"> * GNU General Public License for more details.</span>
<a name="l00018"></a>00018 <span class="comment"> *</span>
<a name="l00019"></a>00019 <span class="comment"> * You should have received a copy of the GNU General Public License</span>
<a name="l00020"></a>00020 <span class="comment"> * along with this program; if not, write to the Free Software</span>
<a name="l00021"></a>00021 <span class="comment"> * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA</span>
<a name="l00022"></a>00022 <span class="comment"> *</span>
<a name="l00023"></a>00023 <span class="comment"> *</span>
<a name="l00024"></a>00024 <span class="comment"> * Acknowledgements</span>
<a name="l00025"></a>00025 <span class="comment"> *</span>
<a name="l00026"></a>00026 <span class="comment"> * Many thanks to the following authors and projects whose work has greatly</span>
<a name="l00027"></a>00027 <span class="comment"> * helped the development of this tool.</span>
<a name="l00028"></a>00028 <span class="comment"> *</span>
<a name="l00029"></a>00029 <span class="comment"> *</span>
<a name="l00030"></a>00030 <span class="comment"> * Sebastian Gesemann - dsd2pcm (http://code.google.com/p/dsd2pcm/)</span>
<a name="l00031"></a>00031 <span class="comment"> * SACD Ripper (http://code.google.com/p/sacd-ripper/)</span>
<a name="l00032"></a>00032 <span class="comment"> * Maxim V.Anisiutkin - foo_input_sacd (http://sourceforge.net/projects/sacddecoder/files/)</span>
<a name="l00033"></a>00033 <span class="comment"> * Vladislav Goncharov - foo_input_sacd_hq (http://vladgsound.wordpress.com)</span>
<a name="l00034"></a>00034 <span class="comment"> * Jesus R - www.sonore.us</span>
<a name="l00035"></a>00035 <span class="comment"> *</span>
<a name="l00036"></a>00036 <span class="comment"> */</span>
<a name="l00037"></a>00037 
<a name="l00038"></a>00038 <span class="preprocessor">#include &lt;boost/timer/timer.hpp&gt;</span>
<a name="l00039"></a>00039 <span class="preprocessor">#include &lt;boost/filesystem.hpp&gt;</span>
<a name="l00040"></a>00040 <span class="preprocessor">#include &lt;dsd_decimator.h&gt;</span>
<a name="l00041"></a>00041 <span class="preprocessor">#include &lt;dsf_file_reader.h&gt;</span>
<a name="l00042"></a>00042 <span class="preprocessor">#include &lt;dsdiff_file_reader.h&gt;</span>
<a name="l00043"></a>00043 <span class="preprocessor">#include &lt;tagConversion.h&gt;</span>
<a name="l00044"></a>00044 <span class="preprocessor">#include &quot;FLAC++/metadata.h&quot;</span>
<a name="l00045"></a>00045 <span class="preprocessor">#include &quot;FLAC++/encoder.h&quot;</span>
<a name="l00046"></a>00046 <span class="preprocessor">#include &quot;math.h&quot;</span>
<a name="l00047"></a>00047 <span class="preprocessor">#include &quot;<a class="code" href="cmdline_8h.html" title="The header file for the command line option parser generated by GNU Gengetopt version 2...">cmdline.h</a>&quot;</span>
<a name="l00048"></a>00048 <span class="preprocessor">#include &lt;sstream&gt;</span>
<a name="l00049"></a>00049 <span class="preprocessor">#include &quot;dop_packer.h&quot;</span>
<a name="l00050"></a>00050 
<a name="l00051"></a>00051 <span class="preprocessor">#define flacBlockLen 100</span>
<a name="l00052"></a>00052 <span class="preprocessor"></span>
<a name="l00053"></a>00053 <span class="keyword">using</span> boost::timer::cpu_timer;
<a name="l00054"></a>00054 <span class="keyword">using</span> boost::timer::cpu_times;
<a name="l00055"></a>00055 <span class="keyword">using</span> boost::timer::nanosecond_type;
<a name="l00056"></a>00056 
<a name="l00057"></a>00057 <span class="keyword">static</span> nanosecond_type reportInterval(100000000LL);
<a name="l00058"></a>00058 <span class="keyword">static</span> cpu_timer timer;
<a name="l00059"></a>00059 <span class="keyword">static</span> dsf2flac_float64 lastPos;
<a name="l00060"></a>00060 
<a name="l00066"></a>00066 <span class="keywordtype">void</span> setupTimer(dsf2flac_float64 currPos)
<a name="l00067"></a>00067 {
<a name="l00068"></a>00068         timer = cpu_timer();
<a name="l00069"></a>00069         lastPos = currPos;
<a name="l00070"></a>00070 }
<a name="l00071"></a>00071 
<a name="l00077"></a>00077 <span class="keywordtype">void</span> checkTimer(dsf2flac_float64 currPos, dsf2flac_float64 percent)
<a name="l00078"></a>00078 {
<a name="l00079"></a>00079         cpu_times <span class="keyword">const</span> elapsed_times(timer.elapsed());
<a name="l00080"></a>00080         nanosecond_type <span class="keyword">const</span> elapsed(elapsed_times.system + elapsed_times.user);
<a name="l00081"></a>00081         <span class="keywordflow">if</span> (elapsed &gt;= reportInterval) {
<a name="l00082"></a>00082                 printf(<span class="stringliteral">&quot;\33[2K\r&quot;</span>);
<a name="l00083"></a>00083                 printf(<span class="stringliteral">&quot;Rate: %4.1fx\t&quot;</span>,(currPos-lastPos)/elapsed*1000000000);
<a name="l00084"></a>00084                 printf(<span class="stringliteral">&quot;Progress: %3.0f%%&quot;</span>,percent);
<a name="l00085"></a>00085                 fflush(stdout);
<a name="l00086"></a>00086                 lastPos = currPos;
<a name="l00087"></a>00087                 timer = cpu_timer();
<a name="l00088"></a>00088         }
<a name="l00089"></a>00089 }
<a name="l00090"></a>00090 
<a name="l00096"></a>00096 boost::filesystem::path muti_track_name_helper(boost::filesystem::path outpath, <span class="keywordtype">int</span> n) {
<a name="l00097"></a>00097         std::ostringstream prefix;
<a name="l00098"></a>00098         prefix &lt;&lt; <span class="stringliteral">&quot;track &quot;</span>;
<a name="l00099"></a>00099         prefix &lt;&lt; n+1;
<a name="l00100"></a>00100         prefix &lt;&lt; <span class="stringliteral">&quot; - &quot;</span>;
<a name="l00101"></a>00101         boost::filesystem::path trackOutPath = outpath.parent_path() / (prefix.str() + outpath.filename().string());
<a name="l00102"></a>00102         <span class="keywordflow">return</span> trackOutPath;
<a name="l00103"></a>00103 }
<a name="l00104"></a>00104 
<a name="l00111"></a>00111 <span class="keywordtype">int</span> pcm_track_helper(
<a name="l00112"></a>00112         boost::filesystem::path outpath,
<a name="l00113"></a>00113         dsdDecimator* dec,
<a name="l00114"></a>00114         <span class="keywordtype">int</span> bits,
<a name="l00115"></a>00115         dsf2flac_float64 scale,
<a name="l00116"></a>00116         dsf2flac_float64 tpdfDitherPeakAmplitude,
<a name="l00117"></a>00117         dsf2flac_float64 startPos,
<a name="l00118"></a>00118         dsf2flac_float64 endPos,
<a name="l00119"></a>00119         ID3_Tag id3tag)
<a name="l00120"></a>00120 {
<a name="l00121"></a>00121         
<a name="l00122"></a>00122         <span class="keywordflow">if</span> ( startPos &gt; dec-&gt;getLength()-1 )
<a name="l00123"></a>00123                 startPos = dec-&gt;getLength()-1;
<a name="l00124"></a>00124                 
<a name="l00125"></a>00125         <span class="keywordflow">if</span> ( endPos &gt; dec-&gt;getLength() )
<a name="l00126"></a>00126                 endPos = dec-&gt;getLength();
<a name="l00127"></a>00127         
<a name="l00128"></a>00128         <span class="comment">// flac vars</span>
<a name="l00129"></a>00129         <span class="keywordtype">bool</span> ok = <span class="keyword">true</span>;
<a name="l00130"></a>00130         FLAC::Encoder::File encoder;
<a name="l00131"></a>00131         FLAC__StreamEncoderInitStatus init_status;
<a name="l00132"></a>00132         FLAC__StreamMetadata *metadata[2];
<a name="l00133"></a>00133 
<a name="l00134"></a>00134         <span class="comment">// setup the encoder</span>
<a name="l00135"></a>00135         <span class="keywordflow">if</span>(!encoder) {
<a name="l00136"></a>00136                 fprintf(stderr, <span class="stringliteral">&quot;ERROR: allocating encoder\n&quot;</span>);
<a name="l00137"></a>00137                 <span class="keywordflow">return</span> 0;
<a name="l00138"></a>00138         }
<a name="l00139"></a>00139         ok &amp;= encoder.set_verify(<span class="keyword">true</span>);
<a name="l00140"></a>00140         ok &amp;= encoder.set_compression_level(5);
<a name="l00141"></a>00141         ok &amp;= encoder.set_channels(dec-&gt;getNumChannels());
<a name="l00142"></a>00142         ok &amp;= encoder.set_bits_per_sample(bits);
<a name="l00143"></a>00143         ok &amp;= encoder.set_sample_rate(dec-&gt;getOutputSampleRate());
<a name="l00144"></a>00144         ok &amp;= encoder.set_total_samples_estimate(endPos - startPos);
<a name="l00145"></a>00145 
<a name="l00146"></a>00146         <span class="comment">// add tags and a padding block</span>
<a name="l00147"></a>00147         <span class="keywordflow">if</span>(ok) {
<a name="l00148"></a>00148                 metadata[0] = id3v2_to_flac( id3tag );
<a name="l00149"></a>00149                 metadata[1] = FLAC__metadata_object_new(FLAC__METADATA_TYPE_PADDING);
<a name="l00150"></a>00150                 metadata[1]-&gt;length = 2048; <span class="comment">/* set the padding length */</span>
<a name="l00151"></a>00151                 ok = encoder.set_metadata(metadata, 2);
<a name="l00152"></a>00152         }
<a name="l00153"></a>00153         
<a name="l00154"></a>00154         <span class="comment">// initialize encoder</span>
<a name="l00155"></a>00155         <span class="keywordflow">if</span>(ok) {
<a name="l00156"></a>00156                 init_status = encoder.init(outpath.c_str());
<a name="l00157"></a>00157                 <span class="keywordflow">if</span>(init_status != FLAC__STREAM_ENCODER_INIT_STATUS_OK) {
<a name="l00158"></a>00158                         fprintf(stderr, <span class="stringliteral">&quot;ERROR: initializing encoder: %s\n&quot;</span>, FLAC__StreamEncoderInitStatusString[init_status]);
<a name="l00159"></a>00159                         ok = <span class="keyword">false</span>;
<a name="l00160"></a>00160                 }
<a name="l00161"></a>00161         }
<a name="l00162"></a>00162 
<a name="l00163"></a>00163         <span class="comment">// return if there is a problem with any of the flac stuff.</span>
<a name="l00164"></a>00164         <span class="keywordflow">if</span> (!ok)
<a name="l00165"></a>00165                 <span class="keywordflow">return</span> ok;
<a name="l00166"></a>00166 
<a name="l00167"></a>00167         <span class="comment">// creep up to the start point.</span>
<a name="l00168"></a>00168         <span class="keywordflow">while</span> (dec-&gt;getPosition() &lt; startPos) {
<a name="l00169"></a>00169                 dec-&gt;step();
<a name="l00170"></a>00170         }
<a name="l00171"></a>00171         <span class="comment">// create a FLAC__int32 buffer to hold the samples as they are converted</span>
<a name="l00172"></a>00172         <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> bufferLen = dec-&gt;getNumChannels()*flacBlockLen;
<a name="l00173"></a>00173         FLAC__int32* buffer = <span class="keyword">new</span> FLAC__int32[bufferLen];
<a name="l00174"></a>00174         <span class="comment">// MAIN CONVERSION LOOP //</span>
<a name="l00175"></a>00175         <span class="keywordflow">while</span> (dec-&gt;getPosition() &lt;= endPos-flacBlockLen) {
<a name="l00176"></a>00176                 dec-&gt;getSamples(buffer,bufferLen,scale,tpdfDitherPeakAmplitude);
<a name="l00177"></a>00177                 <span class="keywordflow">if</span>(!(ok = encoder.process_interleaved(buffer, flacBlockLen)))
<a name="l00178"></a>00178                         fprintf(stderr, <span class="stringliteral">&quot;   state: %s\n&quot;</span>, encoder.get_state().resolved_as_cstring(encoder));
<a name="l00179"></a>00179                 checkTimer(dec-&gt;getPositionInSeconds(),dec-&gt;getPositionAsPercent());
<a name="l00180"></a>00180         }
<a name="l00181"></a>00181         <span class="comment">// delete the old buffer and make a new one with a single sample per chan</span>
<a name="l00182"></a>00182         <span class="keyword">delete</span>[] buffer;
<a name="l00183"></a>00183         buffer = <span class="keyword">new</span> FLAC__int32[dec-&gt;getNumChannels()];
<a name="l00184"></a>00184         <span class="comment">// creep up to the end</span>
<a name="l00185"></a>00185         <span class="keywordflow">while</span> (dec-&gt;getPosition() &lt;= endPos) {
<a name="l00186"></a>00186                 dec-&gt;getSamples(buffer,dec-&gt;getNumChannels(),scale,tpdfDitherPeakAmplitude);
<a name="l00187"></a>00187                 <span class="keywordflow">if</span>(!(ok = encoder.process_interleaved(buffer, 1)))
<a name="l00188"></a>00188                         fprintf(stderr, <span class="stringliteral">&quot;   state: %s\n&quot;</span>, encoder.get_state().resolved_as_cstring(encoder));
<a name="l00189"></a>00189                 checkTimer(dec-&gt;getPositionInSeconds(),dec-&gt;getPositionAsPercent());
<a name="l00190"></a>00190         }
<a name="l00191"></a>00191         <span class="keyword">delete</span>[] buffer;
<a name="l00192"></a>00192         <span class="comment">// close the flac file</span>
<a name="l00193"></a>00193         ok &amp;= encoder.finish();
<a name="l00194"></a>00194         <span class="comment">// report back to the user</span>
<a name="l00195"></a>00195         printf(<span class="stringliteral">&quot;\33[2K\r&quot;</span>);
<a name="l00196"></a>00196         printf(<span class="stringliteral">&quot;%3.1f%%\t&quot;</span>,dec-&gt;getPositionAsPercent());
<a name="l00197"></a>00197         <span class="keywordflow">if</span> (ok) {
<a name="l00198"></a>00198                 printf(<span class="stringliteral">&quot;Conversion completed sucessfully.\n&quot;</span>);
<a name="l00199"></a>00199         } <span class="keywordflow">else</span> {
<a name="l00200"></a>00200                 printf(<span class="stringliteral">&quot;\nError during conversion.\n&quot;</span>);
<a name="l00201"></a>00201                 fprintf(stderr, <span class="stringliteral">&quot;encoding: %s\n&quot;</span>, ok? <span class="stringliteral">&quot;succeeded&quot;</span> : <span class="stringliteral">&quot;FAILED&quot;</span>);
<a name="l00202"></a>00202                 fprintf(stderr, <span class="stringliteral">&quot;   state: %s\n&quot;</span>, encoder.get_state().resolved_as_cstring(encoder));
<a name="l00203"></a>00203         }
<a name="l00204"></a>00204         <span class="comment">// free things</span>
<a name="l00205"></a>00205         FLAC__metadata_object_delete(metadata[0]);
<a name="l00206"></a>00206         FLAC__metadata_object_delete(metadata[1]);
<a name="l00207"></a>00207 
<a name="l00208"></a>00208         <span class="keywordflow">return</span> ok;
<a name="l00209"></a>00209 }
<a name="l00210"></a>00210 
<a name="l00211"></a>00211 <span class="comment">/*</span>
<a name="l00212"></a>00212 <span class="comment"> * do_pcm_conversion</span>
<a name="l00213"></a>00213 <span class="comment"> *</span>
<a name="l00214"></a>00214 <span class="comment"> * this function uses a dsdDecimator to do the conversion into PCM.</span>
<a name="l00215"></a>00215 <span class="comment"> */</span>
<a name="l00216"></a>00216 <span class="keywordtype">int</span> do_pcm_conversion(
<a name="l00217"></a>00217                 DsdSampleReader* dsr,
<a name="l00218"></a>00218                 <span class="keywordtype">int</span> fs,
<a name="l00219"></a>00219                 <span class="keywordtype">int</span> bits,
<a name="l00220"></a>00220                 <span class="keywordtype">bool</span> dither,
<a name="l00221"></a>00221                 dsf2flac_float64 userScale,
<a name="l00222"></a>00222                 boost::filesystem::path inpath,
<a name="l00223"></a>00223                 boost::filesystem::path outpath
<a name="l00224"></a>00224                 )
<a name="l00225"></a>00225 {
<a name="l00226"></a>00226 
<a name="l00227"></a>00227         <span class="keywordtype">bool</span> ok = <span class="keyword">true</span>;
<a name="l00228"></a>00228 
<a name="l00229"></a>00229         <span class="comment">// create decimator</span>
<a name="l00230"></a>00230         dsdDecimator dec(dsr,fs);
<a name="l00231"></a>00231         <span class="keywordflow">if</span> (!dec.isValid()) {
<a name="l00232"></a>00232                 printf(<span class="stringliteral">&quot;%s\n&quot;</span>,dec.getErrorMsg().c_str());
<a name="l00233"></a>00233                 <span class="keywordflow">return</span> 0;
<a name="l00234"></a>00234         }
<a name="l00235"></a>00235 
<a name="l00236"></a>00236         <span class="comment">// calc real scale and dither amplitude</span>
<a name="l00237"></a>00237         dsf2flac_float64 scale = userScale * pow(2.0,bits-1); <span class="comment">// increase scale by factor of 2^23 (24bit).</span>
<a name="l00238"></a>00238         dsf2flac_float64 tpdfDitherPeakAmplitude;
<a name="l00239"></a>00239         <span class="keywordflow">if</span> (dither)
<a name="l00240"></a>00240                 tpdfDitherPeakAmplitude = 1.0;
<a name="l00241"></a>00241         <span class="keywordflow">else</span>
<a name="l00242"></a>00242                 tpdfDitherPeakAmplitude = 0.0;
<a name="l00243"></a>00243 
<a name="l00244"></a>00244         setupTimer(dsr-&gt;getPositionInSeconds());
<a name="l00245"></a>00245 
<a name="l00246"></a>00246         <span class="comment">// convert each track in the file in turn</span>
<a name="l00247"></a>00247         <span class="keywordflow">for</span> (dsf2flac_uint32 n = 0; n &lt; dsr-&gt;getNumTracks();n++) {
<a name="l00248"></a>00248 
<a name="l00249"></a>00249                 <span class="comment">// get and check the start and end samples</span>
<a name="l00250"></a>00250                 dsf2flac_float64 trackStart = (dsf2flac_float64)dsr-&gt;getTrackStart(n) / dec.getDecimationRatio();
<a name="l00251"></a>00251                 dsf2flac_float64 trackEnd = (dsf2flac_float64)dsr-&gt;getTrackEnd(n) / dec.getDecimationRatio();
<a name="l00252"></a>00252                 <span class="keywordflow">if</span> (trackStart &lt; dec.getFirstValidSample())
<a name="l00253"></a>00253                         trackStart = dec.getFirstValidSample();
<a name="l00254"></a>00254                 <span class="keywordflow">if</span> (trackStart &gt;= dec.getLastValidSample() )
<a name="l00255"></a>00255                         trackStart = dec.getLastValidSample() - 1;
<a name="l00256"></a>00256                 <span class="keywordflow">if</span> (trackEnd &lt;= dec.getFirstValidSample())
<a name="l00257"></a>00257                         trackEnd = dec.getFirstValidSample() + 1;
<a name="l00258"></a>00258                 <span class="keywordflow">if</span> (trackEnd &gt; dec.getLastValidSample())
<a name="l00259"></a>00259                         trackEnd = dec.getLastValidSample();
<a name="l00260"></a>00260 
<a name="l00261"></a>00261                 <span class="comment">// construct an appropriate filename for multi track files.</span>
<a name="l00262"></a>00262                 boost::filesystem::path trackOutPath;
<a name="l00263"></a>00263                 <span class="keywordflow">if</span> (dsr-&gt;getNumTracks() &gt; 1) {
<a name="l00264"></a>00264                         trackOutPath = muti_track_name_helper(outpath,n);
<a name="l00265"></a>00265                 } <span class="keywordflow">else</span> {
<a name="l00266"></a>00266                         trackOutPath = outpath;
<a name="l00267"></a>00267                 }
<a name="l00268"></a>00268 
<a name="l00269"></a>00269                 printf(<span class="stringliteral">&quot;Output file\n\t%s\n&quot;</span>,trackOutPath.c_str());
<a name="l00270"></a>00270                 <span class="comment">// use the pcm_track_helper</span>
<a name="l00271"></a>00271                 ok &amp;= pcm_track_helper(trackOutPath,&amp;dec,bits,scale,tpdfDitherPeakAmplitude,trackStart,trackEnd,dsr-&gt;getID3Tag(n));
<a name="l00272"></a>00272         }
<a name="l00273"></a>00273 
<a name="l00274"></a>00274         <span class="keywordflow">return</span> ok;
<a name="l00275"></a>00275 }
<a name="l00276"></a>00276 
<a name="l00280"></a>00280 <span class="keywordtype">int</span> dop_track_helper(
<a name="l00281"></a>00281         boost::filesystem::path outpath,
<a name="l00282"></a>00282         DsdSampleReader* dsr,
<a name="l00283"></a>00283         dsf2flac_int64 startPos,
<a name="l00284"></a>00284         dsf2flac_int64 endPos,
<a name="l00285"></a>00285         ID3_Tag id3tag)
<a name="l00286"></a>00286 {
<a name="l00287"></a>00287 
<a name="l00288"></a>00288         <span class="comment">// double check the start and end positions!</span>
<a name="l00289"></a>00289         <span class="keywordflow">if</span> ( startPos &gt; dsr-&gt;getLength()-1 )
<a name="l00290"></a>00290                 startPos = dsr-&gt;getLength()-1;
<a name="l00291"></a>00291         <span class="keywordflow">if</span> ( endPos &gt; dsr-&gt;getLength() )
<a name="l00292"></a>00292                 endPos = dsr-&gt;getLength();
<a name="l00293"></a>00293 
<a name="l00294"></a>00294         <span class="comment">// create a dop packer object.</span>
<a name="l00295"></a>00295         <a class="code" href="classDopPacker.html">DopPacker</a> dopp = <a class="code" href="classDopPacker.html">DopPacker</a>(dsr);
<a name="l00296"></a>00296 
<a name="l00297"></a>00297         <span class="comment">// flac vars</span>
<a name="l00298"></a>00298         <span class="keywordtype">bool</span> ok = <span class="keyword">true</span>;
<a name="l00299"></a>00299         FLAC::Encoder::File encoder;
<a name="l00300"></a>00300         FLAC__StreamEncoderInitStatus init_status;
<a name="l00301"></a>00301         FLAC__StreamMetadata *metadata[2];
<a name="l00302"></a>00302 
<a name="l00303"></a>00303         <span class="comment">// setup the encoder</span>
<a name="l00304"></a>00304         <span class="keywordflow">if</span>(!encoder) {
<a name="l00305"></a>00305                 fprintf(stderr, <span class="stringliteral">&quot;ERROR: allocating encoder\n&quot;</span>);
<a name="l00306"></a>00306                 <span class="keywordflow">return</span> 0;
<a name="l00307"></a>00307         }
<a name="l00308"></a>00308         ok &amp;= encoder.set_verify(<span class="keyword">true</span>);
<a name="l00309"></a>00309         ok &amp;= encoder.set_compression_level(5);
<a name="l00310"></a>00310         ok &amp;= encoder.set_channels(dsr-&gt;getNumChannels());
<a name="l00311"></a>00311         ok &amp;= encoder.set_bits_per_sample(24);
<a name="l00312"></a>00312 
<a name="l00313"></a>00313         <span class="keywordflow">if</span> ( dsr-&gt;getSamplingFreq() == 2822400 ) {
<a name="l00314"></a>00314                 ok &amp;= encoder.set_sample_rate(176400);
<a name="l00315"></a>00315         } <span class="keywordflow">else</span> <span class="keywordflow">if</span> ( dsr-&gt;getSamplingFreq() == 5644800 ) {
<a name="l00316"></a>00316                 ok &amp;= encoder.set_sample_rate(352800);
<a name="l00317"></a>00317         } <span class="keywordflow">else</span> {
<a name="l00318"></a>00318                 fprintf(stderr, <span class="stringliteral">&quot;ERROR: sample rate not supported by DoP\n&quot;</span>);
<a name="l00319"></a>00319                 <span class="keywordflow">return</span> 0;
<a name="l00320"></a>00320         }
<a name="l00321"></a>00321         ok &amp;= encoder.set_total_samples_estimate((endPos - startPos)/16);
<a name="l00322"></a>00322 
<a name="l00323"></a>00323         <span class="comment">// add tags and a padding block</span>
<a name="l00324"></a>00324         <span class="keywordflow">if</span>(ok) {
<a name="l00325"></a>00325                 metadata[0] = id3v2_to_flac( id3tag );
<a name="l00326"></a>00326                 metadata[1] = FLAC__metadata_object_new(FLAC__METADATA_TYPE_PADDING);
<a name="l00327"></a>00327                 metadata[1]-&gt;length = 2048; <span class="comment">/* set the padding length */</span>
<a name="l00328"></a>00328                 ok = encoder.set_metadata(metadata, 2);
<a name="l00329"></a>00329         }
<a name="l00330"></a>00330 
<a name="l00331"></a>00331         <span class="comment">// initialize encoder</span>
<a name="l00332"></a>00332         <span class="keywordflow">if</span>(ok) {
<a name="l00333"></a>00333                 init_status = encoder.init(outpath.c_str());
<a name="l00334"></a>00334                 <span class="keywordflow">if</span>(init_status != FLAC__STREAM_ENCODER_INIT_STATUS_OK) {
<a name="l00335"></a>00335                         fprintf(stderr, <span class="stringliteral">&quot;ERROR: initializing encoder: %s\n&quot;</span>, FLAC__StreamEncoderInitStatusString[init_status]);
<a name="l00336"></a>00336                         ok = <span class="keyword">false</span>;
<a name="l00337"></a>00337                 }
<a name="l00338"></a>00338         }
<a name="l00339"></a>00339 
<a name="l00340"></a>00340         <span class="comment">// return if there is a problem with any of the flac stuff.</span>
<a name="l00341"></a>00341         <span class="keywordflow">if</span> (!ok)
<a name="l00342"></a>00342                 <span class="keywordflow">return</span> ok;
<a name="l00343"></a>00343 
<a name="l00344"></a>00344         <span class="comment">// creep up to the start point.</span>
<a name="l00345"></a>00345         <span class="keywordflow">while</span> (dsr-&gt;getPosition() &lt; startPos) {
<a name="l00346"></a>00346                 dsr-&gt;step();
<a name="l00347"></a>00347         }
<a name="l00348"></a>00348         <span class="comment">// create a FLAC__int32 buffer to hold the samples as they are converted</span>
<a name="l00349"></a>00349         <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> bufferLen = dsr-&gt;getNumChannels()*flacBlockLen;
<a name="l00350"></a>00350         FLAC__int32* buffer = <span class="keyword">new</span> FLAC__int32[bufferLen];
<a name="l00351"></a>00351         <span class="comment">// MAIN CONVERSION LOOP //</span>
<a name="l00352"></a>00352         <span class="keywordflow">while</span> (dsr-&gt;getPosition() &lt;= endPos-flacBlockLen*16) {
<a name="l00353"></a>00353                 dopp.<a class="code" href="classDopPacker.html#a5afb6411fdfeefc7f3303883c6501cca">pack_buffer</a>(buffer,bufferLen);
<a name="l00354"></a>00354                 <span class="keywordflow">if</span>(!(ok = encoder.process_interleaved(buffer, flacBlockLen)))
<a name="l00355"></a>00355                         fprintf(stderr, <span class="stringliteral">&quot;   state: %s\n&quot;</span>, encoder.get_state().resolved_as_cstring(encoder));
<a name="l00356"></a>00356                 checkTimer(dsr-&gt;getPositionInSeconds(),dsr-&gt;getPositionAsPercent());
<a name="l00357"></a>00357         }
<a name="l00358"></a>00358         <span class="comment">// delete the old buffer and make a new one with a single sample per chan</span>
<a name="l00359"></a>00359         <span class="keyword">delete</span>[] buffer;
<a name="l00360"></a>00360         buffer = <span class="keyword">new</span> FLAC__int32[dsr-&gt;getNumChannels()];
<a name="l00361"></a>00361         <span class="comment">// creep up to the end</span>
<a name="l00362"></a>00362         <span class="keywordflow">while</span> (dsr-&gt;getPosition() &lt;= endPos) {
<a name="l00363"></a>00363                 dopp.<a class="code" href="classDopPacker.html#a5afb6411fdfeefc7f3303883c6501cca">pack_buffer</a>(buffer,dsr-&gt;getNumChannels());
<a name="l00364"></a>00364                 <span class="keywordflow">if</span>(!(ok = encoder.process_interleaved(buffer, 1)))
<a name="l00365"></a>00365                         fprintf(stderr, <span class="stringliteral">&quot;   state: %s\n&quot;</span>, encoder.get_state().resolved_as_cstring(encoder));
<a name="l00366"></a>00366                 checkTimer(dsr-&gt;getPositionInSeconds(),dsr-&gt;getPositionAsPercent());
<a name="l00367"></a>00367         }
<a name="l00368"></a>00368         <span class="keyword">delete</span>[] buffer;
<a name="l00369"></a>00369         <span class="comment">// close the flac file</span>
<a name="l00370"></a>00370         ok &amp;= encoder.finish();
<a name="l00371"></a>00371         <span class="comment">// report back to the user</span>
<a name="l00372"></a>00372         printf(<span class="stringliteral">&quot;\33[2K\r&quot;</span>);
<a name="l00373"></a>00373         printf(<span class="stringliteral">&quot;%3.1f%%\t&quot;</span>,dsr-&gt;getPositionAsPercent());
<a name="l00374"></a>00374         <span class="keywordflow">if</span> (ok) {
<a name="l00375"></a>00375                 printf(<span class="stringliteral">&quot;Conversion completed sucessfully.\n&quot;</span>);
<a name="l00376"></a>00376         } <span class="keywordflow">else</span> {
<a name="l00377"></a>00377                 printf(<span class="stringliteral">&quot;\nError during conversion.\n&quot;</span>);
<a name="l00378"></a>00378                 fprintf(stderr, <span class="stringliteral">&quot;encoding: %s\n&quot;</span>, ok? <span class="stringliteral">&quot;succeeded&quot;</span> : <span class="stringliteral">&quot;FAILED&quot;</span>);
<a name="l00379"></a>00379                 fprintf(stderr, <span class="stringliteral">&quot;   state: %s\n&quot;</span>, encoder.get_state().resolved_as_cstring(encoder));
<a name="l00380"></a>00380         }
<a name="l00381"></a>00381         <span class="comment">// free things</span>
<a name="l00382"></a>00382         FLAC__metadata_object_delete(metadata[0]);
<a name="l00383"></a>00383         FLAC__metadata_object_delete(metadata[1]);
<a name="l00384"></a>00384 
<a name="l00385"></a>00385         <span class="keywordflow">return</span> ok;
<a name="l00386"></a>00386 
<a name="l00387"></a>00387 
<a name="l00388"></a>00388         <span class="keywordflow">return</span> ok;
<a name="l00389"></a>00389 }
<a name="l00396"></a>00396 <span class="keywordtype">int</span> do_dop_conversion(
<a name="l00397"></a>00397                 DsdSampleReader* dsr,
<a name="l00398"></a>00398                 boost::filesystem::path inpath,
<a name="l00399"></a>00399                 boost::filesystem::path outpath
<a name="l00400"></a>00400                 )
<a name="l00401"></a>00401 {
<a name="l00402"></a>00402         <span class="keywordtype">bool</span> ok = <span class="keyword">true</span>;
<a name="l00403"></a>00403 
<a name="l00404"></a>00404         setupTimer(dsr-&gt;getPositionInSeconds());
<a name="l00405"></a>00405 
<a name="l00406"></a>00406         <span class="comment">// convert each track in the file in turn</span>
<a name="l00407"></a>00407         <span class="keywordflow">for</span> (dsf2flac_uint32 n = 0; n &lt; dsr-&gt;getNumTracks();n++) {
<a name="l00408"></a>00408 
<a name="l00409"></a>00409                 <span class="comment">// get and check the start and end samples</span>
<a name="l00410"></a>00410                 dsf2flac_uint64 trackStart = dsr-&gt;getTrackStart(n);
<a name="l00411"></a>00411                 dsf2flac_uint64 trackEnd = dsr-&gt;getTrackEnd(n);
<a name="l00412"></a>00412 
<a name="l00413"></a>00413                 <span class="comment">// construct an appropriate filename for multi track files.</span>
<a name="l00414"></a>00414                 boost::filesystem::path trackOutPath;
<a name="l00415"></a>00415                 <span class="keywordflow">if</span> (dsr-&gt;getNumTracks() &gt; 1) {
<a name="l00416"></a>00416                         trackOutPath = muti_track_name_helper(outpath,n);
<a name="l00417"></a>00417                 } <span class="keywordflow">else</span> {
<a name="l00418"></a>00418                         trackOutPath = outpath;
<a name="l00419"></a>00419                 }
<a name="l00420"></a>00420 
<a name="l00421"></a>00421                 printf(<span class="stringliteral">&quot;Output file\n\t%s\n&quot;</span>,trackOutPath.c_str());
<a name="l00422"></a>00422                 <span class="comment">// use the pcm_track_helper</span>
<a name="l00423"></a>00423                 ok &amp;= dop_track_helper(trackOutPath,dsr,trackStart,trackEnd,dsr-&gt;getID3Tag(n));
<a name="l00424"></a>00424         }
<a name="l00425"></a>00425 
<a name="l00426"></a>00426         <span class="keywordflow">return</span> ok;
<a name="l00427"></a>00427 }
<a name="l00428"></a>00428 
<a name="l00434"></a>00434 <span class="keywordtype">int</span> main(<span class="keywordtype">int</span> argc, <span class="keywordtype">char</span> **argv)
<a name="l00435"></a>00435 {
<a name="l00436"></a>00436         <span class="comment">// use the cmdline processor</span>
<a name="l00437"></a>00437         <a class="code" href="structgengetopt__args__info.html" title="Where the command line options are stored.">gengetopt_args_info</a> args_info;
<a name="l00438"></a>00438         <span class="keywordflow">if</span> (cmdline_parser (argc, argv, &amp;args_info) != 0)
<a name="l00439"></a>00439                 exit(1) ;
<a name="l00440"></a>00440 
<a name="l00441"></a>00441         <span class="comment">// if help or version given then exit now.</span>
<a name="l00442"></a>00442         <span class="keywordflow">if</span> (args_info.<a class="code" href="structgengetopt__args__info.html#ab9fd677f890731fd7d6f6c62e6dfc99c" title="Whether help was given.">help_given</a> || args_info.<a class="code" href="structgengetopt__args__info.html#ad4953a2130b2f8b94a3a687014f278e1" title="Whether version was given.">version_given</a>)
<a name="l00443"></a>00443                 exit(1);
<a name="l00444"></a>00444 
<a name="l00445"></a>00445         <span class="comment">// collect the options</span>
<a name="l00446"></a>00446         <span class="keywordtype">int</span> fs = args_info.<a class="code" href="structgengetopt__args__info.html#a25dd67a0b1f639592b6da55d2b4a18fa" title="Output sample rate (default=&#39;88200&#39;).">samplerate_arg</a>;
<a name="l00447"></a>00447         <span class="keywordtype">int</span> bits = args_info.<a class="code" href="structgengetopt__args__info.html#a0927f8c40479cfd3e08cfa2e1237423b" title="Output bitdepth (default=&#39;24&#39;).">bits_arg</a>;
<a name="l00448"></a>00448         <span class="keywordtype">bool</span> dither = !args_info.<a class="code" href="structgengetopt__args__info.html#a49f3cb35a90c2fa1bbdfd8f3bd7ada11" title="Don&#39;t add dither before quantization (default=off).">nodither_flag</a>;
<a name="l00449"></a>00449         <span class="keywordtype">bool</span> dop = args_info.<a class="code" href="structgengetopt__args__info.html#a46f69892dca29fd7bc7c8f1632cd0fad" title="Encode DSD data directly into FLAC file without conversion to PCM using DoP format (DSD over PCM) (de...">dop_flag</a>;
<a name="l00450"></a>00450         dsf2flac_float64 userScaleDB = (dsf2flac_float64) args_info.<a class="code" href="structgengetopt__args__info.html#a61233d08398ce62edb66aa255c07ecd8" title="Scale adjustment. Raw DSD has a modulation depth of approximately 0.5 so with no scaling the PCM peak...">scale_arg</a>;
<a name="l00451"></a>00451         dsf2flac_float64 userScale = pow(10.0,userScaleDB/20);
<a name="l00452"></a>00452         boost::filesystem::path inpath(args_info.<a class="code" href="structgengetopt__args__info.html#ad093809558d0533ae7834affd91b8a2d" title="Input DSF or DFF file.">infile_arg</a>);
<a name="l00453"></a>00453         boost::filesystem::path outpath;
<a name="l00454"></a>00454         <span class="keywordflow">if</span> (args_info.<a class="code" href="structgengetopt__args__info.html#af6f058929187956cb68a0192b866f942" title="Whether outfile was given.">outfile_given</a>)
<a name="l00455"></a>00455                 outpath = args_info.<a class="code" href="structgengetopt__args__info.html#ab9e3d63af39beb195c0ce4b30628475b" title="Output FLAC file, if not specified the output file be the same as the input file with the extension c...">outfile_arg</a>;
<a name="l00456"></a>00456         <span class="keywordflow">else</span> {
<a name="l00457"></a>00457                 outpath = inpath;
<a name="l00458"></a>00458                 outpath.replace_extension(<span class="stringliteral">&quot;.flac&quot;</span>);
<a name="l00459"></a>00459         }
<a name="l00460"></a>00460 
<a name="l00461"></a>00461         printf(<span class="stringliteral">&quot;%s &quot;</span>,CMDLINE_PARSER_PACKAGE_NAME);
<a name="l00462"></a>00462         printf(<span class="stringliteral">&quot;%s\n\n&quot;</span>,CMDLINE_PARSER_VERSION);
<a name="l00463"></a>00463 
<a name="l00464"></a>00464         <span class="comment">// pointer to the dsdSampleReader (could be any valid type).</span>
<a name="l00465"></a>00465         DsdSampleReader* dsr;
<a name="l00466"></a>00466 
<a name="l00467"></a>00467         <span class="comment">// create either a reader for dsf or dsd</span>
<a name="l00468"></a>00468         <span class="keywordflow">if</span> (inpath.extension() == <span class="stringliteral">&quot;.dsf&quot;</span> || inpath.extension() == <span class="stringliteral">&quot;.DSF&quot;</span>)
<a name="l00469"></a>00469                 dsr = <span class="keyword">new</span> dsfFileReader((<span class="keywordtype">char</span>*)inpath.c_str());
<a name="l00470"></a>00470         <span class="keywordflow">else</span> <span class="keywordflow">if</span> (inpath.extension() == <span class="stringliteral">&quot;.dff&quot;</span> || inpath.extension() == <span class="stringliteral">&quot;.DFF&quot;</span>)
<a name="l00471"></a>00471                 dsr = <span class="keyword">new</span> dsdiffFileReader((<span class="keywordtype">char</span>*)inpath.c_str());
<a name="l00472"></a>00472         <span class="keywordflow">else</span> {
<a name="l00473"></a>00473                 printf(<span class="stringliteral">&quot;Sorry, only .dff or .dff input files are supported\n&quot;</span>);
<a name="l00474"></a>00474                 <span class="keywordflow">return</span> 0;
<a name="l00475"></a>00475         }
<a name="l00476"></a>00476 
<a name="l00477"></a>00477         <span class="comment">// check reader is valid.</span>
<a name="l00478"></a>00478         <span class="keywordflow">if</span> (!dsr-&gt;isValid()) {
<a name="l00479"></a>00479                 printf(<span class="stringliteral">&quot;Error opening DSDFF file!\n&quot;</span>);
<a name="l00480"></a>00480                 printf(<span class="stringliteral">&quot;%s\n&quot;</span>,dsr-&gt;getErrorMsg().c_str());
<a name="l00481"></a>00481                 <span class="keywordflow">return</span> 0;
<a name="l00482"></a>00482         }
<a name="l00483"></a>00483         
<a name="l00484"></a>00484         <span class="comment">// do the conversion into PCM or DoP</span>
<a name="l00485"></a>00485         <span class="keywordflow">if</span> (!dop) {
<a name="l00486"></a>00486                 <span class="comment">// feedback some info to the user</span>
<a name="l00487"></a>00487                 printf(<span class="stringliteral">&quot;Input file\n\t%s\n&quot;</span>,inpath.c_str());
<a name="l00488"></a>00488                 printf(<span class="stringliteral">&quot;Output format\n\tSampleRate: %dHz\n\tDepth: %dbit\n\tDither: %s\n\tScale: %1.1fdB\n&quot;</span>,fs, bits, (dither)?<span class="stringliteral">&quot;true&quot;</span>:<span class="stringliteral">&quot;false&quot;</span>,userScaleDB);
<a name="l00489"></a>00489                 <span class="comment">//printf(&quot;\tIdleSample: 0x%02x\n&quot;,dsr-&gt;getIdleSample());</span>
<a name="l00490"></a>00490 
<a name="l00491"></a>00491                 <span class="keywordflow">return</span> do_pcm_conversion(dsr,fs,bits,dither,userScale,inpath,outpath);
<a name="l00492"></a>00492         } <span class="keywordflow">else</span> {
<a name="l00493"></a>00493                 <span class="comment">// feedback some info to the user</span>
<a name="l00494"></a>00494                 printf(<span class="stringliteral">&quot;Input file\n\t%s\n&quot;</span>,inpath.c_str());
<a name="l00495"></a>00495                 printf(<span class="stringliteral">&quot;Output format\n\tDSD samples packed as DoP\n&quot;</span>);
<a name="l00496"></a>00496 
<a name="l00497"></a>00497                 <span class="keywordflow">return</span> do_dop_conversion(dsr,inpath,outpath);
<a name="l00498"></a>00498         }
<a name="l00499"></a>00499 }
</pre></div></div><!-- contents -->
<!-- window showing the filter options -->
<div id="MSearchSelectWindow"
     onmouseover="return searchBox.OnSearchSelectShow()"
     onmouseout="return searchBox.OnSearchSelectHide()"
     onkeydown="return searchBox.OnSearchSelectKey(event)">
<a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(0)"><span class="SelectionMark">&#160;</span>All</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(1)"><span class="SelectionMark">&#160;</span>Classes</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(2)"><span class="SelectionMark">&#160;</span>Files</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(3)"><span class="SelectionMark">&#160;</span>Functions</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(4)"><span class="SelectionMark">&#160;</span>Variables</a></div>

<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<iframe src="javascript:void(0)" frameborder="0" 
        name="MSearchResults" id="MSearchResults">
</iframe>
</div>



<hr class="footer"/><address class="footer"><small>
Generated on Fri Sep 13 2013 14:42:01 for DSF2FLAC by &#160;<a href="http://www.doxygen.org/index.html">
<img class="footer" src="doxygen.png" alt="doxygen"/>
</a> 1.7.6.1
</small></address>

</body>
</html>
